package com.module.product;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import base.util.ConstantUnit;
import base.util.Page;
import base.util.StringUtil;

import com.module.product.manager.AttrItemManager;
import com.module.product.manager.ProductManager;
import com.module.product.vo.AttrItem;
import com.module.sys.LoginSession;

public class AttrItemService {
	private AttrItemManager attrItemManager=AttrItemManager.getInstance();
	public Page list(AttrItem obj, Integer start, Integer limit) throws Exception {
		Page page = new Page();
		page.setStart(start);
		page.setLimit(limit);
		return attrItemManager.getList(obj, page);
	}

	
	public List<AttrItem> listByGroupId(String groupId, String productId) {
		try {
			AttrItem obj = new AttrItem();
			obj.setAttrGroup(groupId);
			obj.setProductId(productId);
			return attrItemManager.listByGroupId(obj);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	
	
	public AttrItem findbyid(String id) {
		try {
			return attrItemManager.getAttrItem(id);
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}

	
	
	
	public Map save(AttrItem obj) {
		try {
			if (StringUtils.isBlank(obj.getId()))// 添加
			{
				obj.setCreaterId(LoginSession.getUserid());
			} else// 修改
			{
				obj.setUpdaterId(LoginSession.getUserid());
			}
			boolean isTrue=attrItemManager.add(obj);
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("state", isTrue);
			if (isTrue == true) {
				map.put("msg", "保存成功");
			} else {
				map.put("msg", "保存失败");
			}
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public Map<String, Object> remove(List<String> idArray) throws Exception {
		String ids = StringUtil.getNewStrByList(idArray);
		AttrItem obj = new AttrItem();
		obj.setId(ids);
		obj.setUpdaterId(LoginSession.getUserid());
		boolean flag=ProductManager.getInstance().getCountByAttrGrop(obj.getAttrGroup());
		 Map<String, Object> statusMap = new HashMap<String, Object>();
		if(!flag){
			boolean isTrue=attrItemManager.delete(obj);
			 statusMap.put("state", isTrue);
			 if(isTrue == true) {
					statusMap.put("msg", "删除成功");
			 } else {
					statusMap.put("msg", "删除失败");
			 }
		}else{
			statusMap.put("state", false);
			statusMap.put("msg", "属性已被产品引用，不能删除");
		}
		return statusMap;
	}
}
